Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save narendrakamadi/8297942 to your computer and use it in GitHub Desktop.
Save narendrakamadi/8297942 to your computer and use it in GitHub Desktop.

Revisions

  1. @rakeshtembhurne rakeshtembhurne created this gist Jan 3, 2014.
    22 changes: 22 additions & 0 deletions cakephp_get_list_data_in_json.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    <?php
    /**
    * Gets list from various models
    *
    * @param string $modelName Model name
    * @param string $key field name used in condition
    * @param string $value value of field used in conditions
    *
    * @return json
    */
    public function get_list($modelName, $key = null, $value = null)
    {
    $this->autoLayout = false;
    $this->autoRender = false;
    $options = array('is_active' => 1);

    if (!empty($key) && !empty($value)) $options['conditions'] = array("{$modelName}.{$key}" => $value);
    $data = ClassRegistry::init($modelName)->find('list', $options);

    echo json_encode($data);

    }//edn get_list()
    21 changes: 21 additions & 0 deletions cakephp_show_json_list.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    $("form").on("change", "#UserSiteId", function(event) {
    var siteId = $(this).val();

    // Call CakePHP get_list function here
    $.post(window.app.baseUrl + "users/get_list/Wing/site_id/" + siteId, function(response) {
    var data = JSON.parse(response);

    // Empty initially
    var wingOptions = "";
    $userWingId.html("");

    // Inject data
    for (var key in data) {
    wingOptions += "<option value=" + key + ">" + data[key] + "</option>";
    };
    $userWingId.html(wingOptions);
    $userWingId.trigger("change");

    });
    });
    $("#UserSiteId").trigger("change");